import { Body, Controller, Get, Post, Res, HttpStatus, UploadedFile, UseInterceptors, HttpException, Query } from '@nestjs/common';
import { FileInterceptor } from '@nestjs/platform-express';
import { Response } from 'express';
import { Setting } from './setting.entity';
import { SettingService } from './setting.service';

import {join, resolve} from 'path'
import { createWriteStream } from 'fs';

import {
  apibefore
} from '../../router'
import { ValidUniqueLoginInterceptor } from 'src/interceptor/validUniqueLogin';

@Controller(apibefore('/setting'))
export class SettingController {
  constructor(private readonly settingService: SettingService) {}

  
  @Get('/get_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async get_auth(@Query() params){
    return await this.settingService.findOne_by_key(params.key)
  }

  
  @Get('/get_auth_all')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async get_auth_all(@Query() params){
    return await this.settingService.findOne_by_key_all(params.key)
  }

  @Get('/get')
  async get(@Query() params){
    return await this.settingService.findOne_by_key(params.key)
  }
  
  @Post('/set_auth')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async set_auth(@Body() data){
    let setting = await this.settingService.findOne_by_key(data.key)
    if(!setting){
      setting = new Setting()
      setting.key = data.key
    }
    setting.value = data.value
    return await this.settingService.create(setting)
  }

  
  @Post('/set_auth_list')
  @UseInterceptors(ValidUniqueLoginInterceptor)
  async set_auth_list(@Body() data){
    return await this.settingService.set_auth_list(data.key, data.value)
  }


  @Post('/set')
  async set(@Body() data){
    let setting = await this.settingService.findOne_by_key(data.key)
    if(!setting){
      setting = new Setting()
      setting.key = data.key
    }
    setting.value = data.value
    return await this.settingService.create(setting)
  }

  @Post('/update')
  update(@Body() settingParams: { id: number; title: string; desc: string, wx_user: number, gm_user: number,video_url: string }) {

  }

  @Get('/findAll')
  async findAll() {

  }

  @Post('/remove')
  async remove(@Body() settingParams: { id: number }){
    return await this.settingService.remove(settingParams.id)
  }

  deleteVideo(FileId: string, then: Function, catchF: Function){

  }
}
